package com.wanxi.servlet;

import com.wanxi.model.BrandModel;
import com.wanxi.service.BrandService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
//        2.得到前端的值
        String name = req.getParameter("name");
        String enterprise = req.getParameter("enterprise");
        String sort = req.getParameter("sort");
        String introduction = req.getParameter("introduction");
        String status = req.getParameter("status");
//        3.封装数据到model
//        BrandModel model = new BrandModel(1, name, enterprise, Integer.valueOf(sort), introduction, Integer.valueOf(status));
//        model.setStatus(Integer.valueOf(status));
//        Tool tool = new Tool();
        BrandModel model = BrandModel.builder()
                .name(name)
                .sort(Tool.strToInt(sort))
                .status(Tool.strToInt(status))
                .enterprise(enterprise)
                .introduction(introduction)
                .build();
//        4.处理逻辑关系
        BrandService brandService = new BrandService();

//        5.得到逻辑处理后的返回值
        String result = brandService.add(model);
//        6.转发数据
//        if(true){
            resp.sendRedirect("/show");
//        }else {
//            resp.sendRedirect("/error.jsp");
//        }
//        req.setAttribute("result", result);
//        req.getRequestDispatcher("/success.jsp").forward(req, resp);
    }
}
